# -*- coding: utf-8 -*- 

###########################################################################
## Python code generated with wxFormBuilder (version Sep  8 2010)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################

import wx
import hashlib
import logging

###########################################################################
## Class Gui_MainFrame
###########################################################################

class Gui_MainFrame (wx.Frame):
    
    def __init__(self, parent):
        wx.Frame.__init__ (self, parent, id=wx.ID_ANY, title=u"Md5 Caculator", pos=wx.DefaultPosition, size=wx.Size(500, 300), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
        
        self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
        
        bSizer1 = wx.BoxSizer(wx.VERTICAL)
        
        self.m_panel1 = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
        bSizer3 = wx.BoxSizer(wx.VERTICAL)
        
        fgSizer1 = wx.FlexGridSizer(2, 2, 0, 0)
        fgSizer1.AddGrowableCol(1)
        fgSizer1.SetFlexibleDirection(wx.BOTH)
        fgSizer1.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_SPECIFIED)
        
        self.m_staticText2 = wx.StaticText(self.m_panel1, wx.ID_ANY, u"Select a file:", wx.DefaultPosition, wx.DefaultSize, 0)
        self.m_staticText2.Wrap(-1)
        fgSizer1.Add(self.m_staticText2, 0, wx.ALL | wx.ALIGN_RIGHT, 5)
        
        self.m_fpFile = wx.FilePickerCtrl(self.m_panel1, wx.ID_ANY, wx.EmptyString, u"Select a file", u"*.*", wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
        fgSizer1.Add(self.m_fpFile, 0, wx.ALL | wx.EXPAND, 5)
        
        self.m_staticText3 = wx.StaticText(self.m_panel1, wx.ID_ANY, u"MD5:", wx.DefaultPosition, wx.DefaultSize, 0)
        self.m_staticText3.Wrap(-1)
        fgSizer1.Add(self.m_staticText3, 0, wx.ALL | wx.ALIGN_RIGHT, 5)
        
        self.m_tcMD5 = wx.TextCtrl(self.m_panel1, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_READONLY)
        fgSizer1.Add(self.m_tcMD5, 0, wx.ALL | wx.EXPAND, 5)
        
        bSizer3.Add(fgSizer1, 1, wx.EXPAND, 5)
        
        self.m_staticline1 = wx.StaticLine(self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL)
        bSizer3.Add(self.m_staticline1, 0, wx.EXPAND | wx.ALL, 5)
        
        bSizer2 = wx.BoxSizer(wx.HORIZONTAL)
        
        self.m_button4 = wx.Button(self.m_panel1, wx.ID_ANY, u"&Caculator", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer2.Add(self.m_button4, 0, wx.ALL, 5)
        
        self.m_button5 = wx.Button(self.m_panel1, wx.ID_ANY, u"&Exit", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer2.Add(self.m_button5, 0, wx.ALL, 5)
        
        bSizer3.Add(bSizer2, 0, wx.ALIGN_RIGHT, 5)
        
        self.m_panel1.SetSizer(bSizer3)
        self.m_panel1.Layout()
        bSizer3.Fit(self.m_panel1)
        bSizer1.Add(self.m_panel1, 1, wx.EXPAND, 5)
        
        self.SetSizer(bSizer1)
        self.Layout()
        
        self.Centre(wx.BOTH)
        
        # Connect Events
        self.m_button4.Bind(wx.EVT_BUTTON, self.OnCaculatClick)
        self.m_button5.Bind(wx.EVT_BUTTON, self.OnExit)
    
#    def __del__(self):
#        print '__del__'
#        pass


    def md5sum(self, path):
        try:
            file_ = open(path, 'rb')
            md5 = hashlib.md5()
            buffer_ = file_.read(2 ** 20)
            while buffer_:
                md5.update(buffer_)
                buffer_ = file_.read(2 ** 20)
            file_.close()
            return md5.hexdigest()
        except:
            logging.exception('md5sum')
            import os
            print('Usage: {0} <filename>'.format(os.path.basename(path)))
        
    
    # Virtual event handlers, overide them in your derived class
    def OnCaculatClick(self, event):
        self.m_tcMD5.Clear()
        if self.m_fpFile.GetPath() == '':
            return
        print self.m_fpFile.GetPath()
        md5_=self.md5sum(self.m_fpFile.GetPath())
        print md5_
        self.m_tcMD5.SetValue(md5_)
    
    def OnExit(self, event):
        print 'OnExit begin'
        self.Close()
        print 'OnExit end'


app = wx.App(False)
frame = Gui_MainFrame(None)
frame.Show()
app.MainLoop()
    

